package com.mxpio.erp.plan.mrp.policy;

import java.util.HashMap;
import java.util.Map;

import com.mxpio.erp.plan.mrp.Context;
import com.mxpio.erp.plan.mrp.CountModelMap;
import com.mxpio.erp.plan.mrp.bo.CountModel;

public abstract class LrpStoragePolicy implements LrpPolicy {
	
	@Override
	public void run(Context context) {
		before(context);
		apply(context);
		after(context);
	}
	
	public void indexProcessAdd(Context context,Map<String, Object> entity, CountModelMap<CountModel> result, CountModel countModel) {
		
		String itemCode = (String) entity.get("itemCode");
		StringBuilder sb = new StringBuilder();
		//遍历索引
		for(String itemIndexField : context.getItemIndexFields()) {
            sb.append(entity.get(itemIndexField)).append(",");
        }
		if(sb.length() != 0) {
			sb.setLength(sb.length() - 1);
		}
			
		countModel.setIndexes(sb.toString());
		
		Map<String, CountModel> resultMap = result.get(itemCode);
		if(resultMap == null) {
			resultMap = new HashMap<>();
		}
		//正负库存相加
		if(resultMap.get(sb.toString()) != null){
			countModel.setCount(countModel.getCount().add(resultMap.get(sb.toString()).getCount()));
		}
		resultMap.put(sb.toString(), countModel);
		result.put(itemCode, resultMap);
	}

}
